<?php

namespace app\admin\controller;

use app\admin\model\Fields;
use app\common\controller\Backend;

/**
 * 无人超市
 */
class Shop extends Backend
{
    /**
     * Shop模型对象
     * @var object
     * @phpstan-var \app\admin\model\Shop
     */
    protected object $model;

    protected array|string $preExcludeFields = ['id'];

    protected string|array $quickSearchField = ['id'];

    protected array $fields = [];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new \app\admin\model\Shop();
        $this->request->filter('clean_xss');
    }

    protected function setExportTitle(): string
    {
        return '无人超市';
    }
    protected function setExportData($data,&$worksheet){
        foreach ($data as $key => $item){
            $row = $key + 2;
            $k = 0;
            
            foreach ($item['datas'] as $field => $value){
                $k += 1;
                $field_config = array_filter($this->fields,function($item) use ($field){
                    return $item['name'] == $field;
                });
                if(count($field_config) == 0){
                    continue;
                }
                $field_config = array_values($field_config)[0];
                if($field_config['designType'] == 'datetime'){
                    $value = date(Fields::getTimeFormat($field_config['table']['timeFormat']['value']),$value);
                }else if($field_config['designType'] == 'select'){
                    $value = $field_config['data']['replaceValue']['label'][$value] ?? $value;
                }
                $worksheet->setCellValue([$k, $row],$value);
            }
        }
    }
    /**
     * 导出表头
     * @return array
     */
    protected function setExportHead(): array
    {
        $head = [];
        $list = Fields::where('name','shop')->field('field_item')->order('id')->select()->toArray();
        $fields = [];
        foreach ($list as $item){
            $fields[] = $item['field_item'];
            $head[] = $item['field_item']['title'];
        }
        $this->fields = $fields;
        return $head;
    }


    public function index(): void
    {
        if ($this->request->param('select')) {
            $this->select();
        }

        list($where, $alias, $limit, $order) = $this->queryBuilder();
        foreach($where as $k => $v){
            //$v[0] 是否包含datas
            if(strpos($v[0],'datas') !== false){
                //$v[0] 中的. 替换为->
                $where[$k][0] = str_replace('.','->',$v[0]);
            }
        }
        
        foreach($order as $k => $v){
            if(strpos($k,'datas') !== false){
                $orderRaw = "JSON_EXTRACT(datas,'$.".ltrim($k,'datas.')."') ". $v;
            }
        }
        $model = $this->model
            ->field($this->indexField)
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where($where);
        if(isset($orderRaw)){
            $res = $model->orderRaw($orderRaw)->paginate($limit);
        }else{
            $res = $model->order($order)->paginate($limit);
        }
       
        $fields = Fields::where('name','shop')->field('field_item')->select()->toArray();
        $fields = array_column($fields,'field_item');
        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => $fields,
        ]);
    }

    /**
     * 若需重写查看、编辑、删除等方法，请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
     */
}